﻿using FastExecutor.Base.Design;
using FastExecutor.Base.Util;
using FastExecutor.ORG.Entity;
using FastExecutor.ORG.Util;
using FastORM.Applicaiton;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FastExecutor.ORG.DB
{
    public class DB_Group:FastTemplateDB<Frame_Group>
    {
        public override void Insert(Frame_Group Entity)
        {
            Entity.RowGuid = CommonUtil.CreateCommonGuid();
            Entity.AddDate = DateTime.Now;
            Entity.GroupCode = DataBuilder.CreateTableFieldCodeValue("Frame_Group", "RowGuid", "GroupCode", "ParentGroupGuid", Entity.ParentGroupGuid);
            base.Insert(Entity);
        }

        public override void Update(Frame_Group Entity)
        {
            Frame_Group DBEntity = FindOne(Entity.RowGuid);
            if (DBEntity.ParentGroupGuid != Entity.ParentGroupGuid)
            {
                Entity.GroupCode = DataBuilder.CreateTableFieldCodeValue("Frame_Group", "RowGuid", "GroupCode", "ParentGroupGuid", Entity.ParentGroupGuid);
            }
            base.Update(Entity);
        }

        public override void Update(Newtonsoft.Json.Linq.JObject JsonEntity)
        {
            Frame_Group DBEntity = FindOne(Convert.ToString(JsonEntity.GetValue("RowGuid")));
            if (DBEntity.ParentGroupGuid != Convert.ToString(JsonEntity.GetValue("ParentGroupGuid")))
            {
                JsonEntity["GroupCode"] = DataBuilder.CreateTableFieldCodeValue("Frame_Group", "RowGuid", "GroupCode", "ParentGroupGuid", Convert.ToString(JsonEntity.GetValue("ParentGroupGuid")));
            }
            base.Update(JsonEntity);
        }

        public override void BatchUpdate(List<Frame_Group> EntityList)
        {
            foreach (Frame_Group Entity in EntityList)
            {
                Frame_Group DBEntity = FindOne(Entity.RowGuid);
                if (DBEntity.ParentGroupGuid != Entity.ParentGroupGuid)
                {
                    Entity.GroupCode = DataBuilder.CreateTableFieldCodeValue("Frame_Group", "RowGuid", "GroupCode", "ParentGroupGuid", Entity.ParentGroupGuid);
                }
            }
            base.BatchUpdate(EntityList);
        }

        public override void BatchUpdate(Newtonsoft.Json.Linq.JArray JsonArray)
        {
            foreach (JObject JsonEntity in JsonArray)
            {
                Frame_Group DBEntity = FindOne(Convert.ToString(JsonEntity.GetValue("RowGuid")));
                if (DBEntity.ParentGroupGuid != Convert.ToString(JsonEntity.GetValue("ParentGroupGuid")))
                {
                    JsonEntity["GroupCode"] = DataBuilder.CreateTableFieldCodeValue("Frame_Group", "RowGuid", "GroupCode", "ParentGroupGuid", Convert.ToString(JsonEntity.GetValue("ParentGroupGuid")));
                }
            }
            base.BatchUpdate(JsonArray);
        }

    }
}
